/* GetWinVer.cpp  Version 1.1 */
/* */
/* Copyright (C) 2001-2003 Hans Dietrich */
/* */
/* This software is released into the public domain. */
/* You are free to use it in any way you like, except */
/* that you may not sell this source code. */
/* */
/* This software is provided "as is" with no expressed */
/* or implied warranty.  I accept no liability for any */
/* damage or loss of business that this software may cause. */
/* */
/* ///////////////////////////////////////////////////////////////////////////// */

#include <crisscross/universal_include.h>
#include <tchar.h>
#include <crisscross/windows_version.h>

#ifndef VER_PLATFORM_WIN32s
#define VER_PLATFORM_WIN32s             0
#endif
#ifndef VER_PLATFORM_WIN32_WINDOWS
#define VER_PLATFORM_WIN32_WINDOWS      1
#endif
#ifndef VER_PLATFORM_WIN32_NT
#define VER_PLATFORM_WIN32_NT           2
#endif
#ifndef VER_PLATFORM_WIN32_CE
#define VER_PLATFORM_WIN32_CE           3
#endif

BOOL GetWinVer(LPTSTR pszVersion, int *nVersion, LPTSTR pszMajorMinorBuild)
{
	if (!pszVersion || !nVersion || !pszMajorMinorBuild)
		return FALSE;

	lstrcpy(pszVersion, WUNKNOWNSTR);
	*nVersion = WUNKNOWN;

	OSVERSIONINFO osinfo;
	osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

	if (!GetVersionEx(&osinfo))
		return FALSE;

	DWORD         dwPlatformId = osinfo.dwPlatformId;
	DWORD         dwMinorVersion = osinfo.dwMinorVersion;
	DWORD         dwMajorVersion = osinfo.dwMajorVersion;
	DWORD         dwBuildNumber = osinfo.dwBuildNumber & 0xFFFF; /* Win 95 needs this */

	wsprintf(pszMajorMinorBuild, _T("%u.%u.%u"), dwMajorVersion, dwMinorVersion, dwBuildNumber);

	if ((dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) && (dwMajorVersion == 4)) {
		if ((dwMinorVersion < 10) && (dwBuildNumber == 950)) {
			lstrcpy(pszVersion, W95STR);
			*nVersion = W95;
		} else if ((dwMinorVersion < 10) &&
		           ((dwBuildNumber > 950) && (dwBuildNumber <= 1080))) {
			lstrcpy(pszVersion, W95SP1STR);
			*nVersion = W95SP1;
		} else if ((dwMinorVersion < 10) && (dwBuildNumber > 1080)) {
			lstrcpy(pszVersion, W95OSR2STR);
			*nVersion = W95OSR2;
		} else if ((dwMinorVersion == 10) && (dwBuildNumber == 1998)) {
			lstrcpy(pszVersion, W98STR);
			*nVersion = W98;
		} else if ((dwMinorVersion == 10) &&
		           ((dwBuildNumber > 1998) && (dwBuildNumber < 2183))) {
			lstrcpy(pszVersion, W98SP1STR);
			*nVersion = W98SP1;
		} else if ((dwMinorVersion == 10) && (dwBuildNumber >= 2183)) {
			lstrcpy(pszVersion, W98SESTR);
			*nVersion = W98SE;
		} else if (dwMinorVersion == 90) {
			lstrcpy(pszVersion, WMESTR);
			*nVersion = WME;
		}
	} else if (dwPlatformId == VER_PLATFORM_WIN32_NT) {
		if ((dwMajorVersion == 3) && (dwMinorVersion == 51)) {
			lstrcpy(pszVersion, WNT351STR);
			*nVersion = WNT351;
		} else if ((dwMajorVersion == 4) && (dwMinorVersion == 0)) {
			lstrcpy(pszVersion, WNT4STR);
			*nVersion = WNT4;
		} else if ((dwMajorVersion == 5) && (dwMinorVersion == 0)) {
			lstrcpy(pszVersion, W2KSTR);
			*nVersion = W2K;
		} else if ((dwMajorVersion == 5) && (dwMinorVersion == 1)) {
			lstrcpy(pszVersion, WXPSTR);
			*nVersion = WXP;
		} else if ((dwMajorVersion == 5) && (dwMinorVersion == 2)) {
			lstrcpy(pszVersion, W2003SERVERSTR);
			*nVersion = W2003SERVER;
		} else if ((dwMajorVersion == 6) && (dwMinorVersion == 0)) {
			lstrcpy(pszVersion, WVISTASTR);
			*nVersion = WVISTA;
		}
	} else if (dwPlatformId == VER_PLATFORM_WIN32_CE) {
		lstrcpy(pszVersion, WCESTR);
		*nVersion = WCE;
	}

	return TRUE;
}
